//填多个链交叉商品，
var result_fs_cross = JSON.parse('{"CODE":0, "ACTION":"","LEVEL":"", "MSG":"","DATA":"","RULE":"FS_CROSS"}');
var JSON_ITEM_LIST = !isNull(ITEM_LIST) ? JSON.parse(ITEM_LIST) : [];
var CROSS_REF = !isNull(JSON_CROSS_REF) ? JSON.parse(JSON_CROSS_REF) : [];
//如果链交叉商品不为空，并且零售和成本不为空，则自增其余链交叉商品，如果itemList中存在多条则去最低的成本零售
function findVNPK(ITEM_NBR,CROSS_REF){
    for (var cri = 0; cri < CROSS_REF.length; cri++) {
        if (ITEM_NBR === CROSS_REF[cri].ITEM_NBR){
            return CROSS_REF[cri].VNPK_QTY;
        }
    }
}
if (!isNull(CROSS_REF) && !isNull(NEW_RETAIL)  && !isNull(JSON_ITEM_LIST)
    && !isNull(AI_FRESH_FLAG) && AI_FRESH_FLAG === 'Y') {


    var crossItemList = [];
    var inItemList = [];
    //过滤不是该链交叉下的商品
    for (var fItem = 0; fItem < JSON_ITEM_LIST.length; fItem++) {
        for (var cr = 0; cr < CROSS_REF.length; cr++) {
            if (JSON_ITEM_LIST[fItem].ITEM_NBR === CROSS_REF[cr].ITEM_NBR) {
                crossItemList.push(JSON_ITEM_LIST[fItem]);
                inItemList.push(JSON_ITEM_LIST[fItem].ITEM_NBR);
            }
        }
    }
    if (crossItemList.length > 0) {

        crossItemList.sort(function (a, b) {
            return a.NEW_RETAIL > b.NEW_RETAIL ? 1 : -1;
        })

        var minCost = crossItemList[0];
        var minQTY = findVNPK(minCost.ITEM_NBR,CROSS_REF);
        var insertMap = new Map();
        for (var jc = 0; jc < CROSS_REF.length; jc++) {
            var crossRefItem = CROSS_REF[jc];
            if (inItemList.indexOf(crossRefItem.ITEM_NBR) === -1) {
                var data = deepCopy(minCost);
                data.ITEM_NBR = crossRefItem.ITEM_NBR;
                if (!isNull(NEW_CASE_COST)){
                    //（当前商品成本/当前商品箱包）* 其他交叉链商品箱包
                    data.NEW_CASE_COST = ((minCost.NEW_CASE_COST /  minQTY) * crossRefItem.VNPK_QTY).toFixed(2);
                }
                data.Other_Reminds = 'New Retail for FreshCross';
                data.NEW_COST_EFF_DATE = getNewDate(new Date());
                data.NEW_COST_EXP_DATE = '2049-12-31';
                data.SYSTEM_GENERATED_UNIQUE = calcMD5("" + data.FORM_ID + data.ITEM_NBR
                    + data.TRAIT_STORE_NBR + data.RETAIL_TYPE +
                    data.NEW_CASE_COST + data.NEW_RETAIL + data.NEW_COST_EFF_DATE + data.NEW_COST_EXP_DATE +
                    data.NEW_RETAIL_EFF_DATE + data.NEW_RETAIL_EXP_DATE);
                data.ID = null;
                if (!insertMap.containsKey(data.SYSTEM_GENERATED_UNIQUE)) {
                    insertMap.put(data.SYSTEM_GENERATED_UNIQUE, data);
                }
            }
        }
        if (insertMap.values() && insertMap.values().length > 0) {
            result_fs_cross.CODE = 5;
            result_fs_cross.ACTION = "INSERTBATCH";
            result_fs_cross.MSG = "result_fs_cross增加多条";
            result_fs_cross.DATA = insertMap.values();
        }
    }
}

JSON.stringify(result_fs_cross);

